package com.three.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.three.entity.Inlibrary;
import com.three.entity.Staff;
import com.three.entity.StorageVolume;
import com.three.mapper.BranchGoodsMapper;
import com.three.mapper.InlibraryMapper;
import com.three.mapper.StorageVolumeMapper;
import com.three.service.IntLibraryService;
import com.three.vo.BranchVo;
import com.three.vo.GoodsidAndCountVo;
import com.three.vo.R;
import com.three.vo.intLibraryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class IntLibraryServiceImpl implements IntLibraryService {
    @Autowired
    private InlibraryMapper intLibraryMapper;
    @Autowired
    private HttpSession session;
    @Autowired
    private StorageVolumeMapper storageVolumeMapper;
    @Autowired
    private InlibraryMapper inlibraryMapper;
    @Autowired
    private BranchGoodsMapper branchGoodsMapper;

    //展示所有入库信息
    @Override
    public R list(Integer pageNum, Integer pageSize, BranchVo branchVo) {
        //开启分页
        PageHelper.startPage(pageNum, pageSize);
        Staff staff = (Staff) session.getAttribute("staff");//获取当前登录用户
        //判断是否是店长
        if (!staff.getBranchid().equals(12138)) {
            //获取当前登录用户的分店id
            List<Inlibrary>  selectbybranchid = intLibraryMapper.selectbybranchid(staff.getBranchid());
            PageInfo<Inlibrary> pageInfo = new PageInfo<>(selectbybranchid);
            return R.ok(pageInfo);
        }
        //分页查询所有入库信息
        List<Inlibrary> inlibraries = intLibraryMapper.selectIntLib(branchVo);
        PageInfo<Inlibrary> pageInfo = new PageInfo<>(inlibraries);
        return R.ok(pageInfo);
    }

    //添加入库信息
    @Override
    public R addIntLibrary(List<intLibraryVO> inlibrary) {
        Staff staff = (Staff) session.getAttribute("staff");//获取当前登录用户
        Inlibrary inlibrary1 = new Inlibrary();//创建入库对象
        inlibrary1.setDate(new Date());//设置入库时间
        inlibrary1.setStatus(1);//设置入库状态
        inlibrary1.setBranchid(staff.getBranchid());//设置分店id
        inlibrary1.setGoodsid(inlibrary.get(0).getGoodsid());//设置商品id
        inlibrary1.setStaffid(staff.getStaffid());//设置入库人id
        int i = intLibraryMapper.insert(inlibrary1);//添加入库信息
        if (i > 0) {
            intLibraryVO intLibraryVO1 = new intLibraryVO();//创建入库对象
            //循环遍历入库商品信息
            for (intLibraryVO intLibraryVO : inlibrary) {
                intLibraryVO1.setId(inlibrary1.getId());//设置入库id
                intLibraryVO1.setGoodsid(intLibraryVO.getGoodsid());//设置商品id
                intLibraryVO1.setNum(intLibraryVO.getRu());//设置入库数量
                intLibraryVO1.setBranchid(staff.getBranchid());//设置分店id
                intLibraryVO1.setCprice(intLibraryVO.getCprice());//设置成本价
                intLibraryVO1.setSumprice(intLibraryVO.getCprice() * intLibraryVO.getRu());//设置总金额
                intLibraryMapper.insertlin(intLibraryVO1);//添加入库商品信息
            }

        }
        return R.ok();
    }

    // 审核通过
    @Override
    @Transactional
    public R approved(Integer id) {//入库id
        Staff staff = (Staff) session.getAttribute("staff");//获取当前登录用户
        Integer branchid = staff.getBranchid();//获取当前登录用户的分店id
        Date date = new Date();//获取当前时间
        int i = inlibraryMapper.updateStateById(id, date);//更新入库状态
        if (i > 0){//审核通过时
            //获取入库商品信息
            List<GoodsidAndCountVo> aaa = storageVolumeMapper.selectCounts(id, branchid);
            //循环遍历入库商品信息
            for (GoodsidAndCountVo bb : aaa){
                Integer goodsid = bb.getGoodsid();//商品id
                Integer num = bb.getNum();//入库数量
                branchGoodsMapper.updateByBranchidAndGoodsid(goodsid, num, branchid);//更新商品数量
            }
            return R.ok();
        }else {
            return R.error("审核失败");
        }
    }


    //审核驳回
    @Override
    public R rejected(Integer id,String remarks) {
        Date date = new Date();//获取当前时间
        int i = intLibraryMapper.updateStateNo(id, remarks, date);//更新入库状态
        if (i > 0){
            return R.ok();
        }else {
            return R.error("审核失败");
        }
    }
///获取当前登录用户的分店id
    @Override
    public R getUserBranchId() {
        Staff staff = (Staff) session.getAttribute("staff");
        Integer branchid = staff.getBranchid();//获取当前登录用户的分店id
        return R.ok(branchid);
    }

    @Override
    public R getBranchByid(Integer id) {
        Integer branchid = intLibraryMapper.selectByPrimaryKey(id).getBranchid();//获取入库分店id
        return R.ok(branchid);

    }

}
